<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="交易日期" prop="tradeDate">-->
      <!--        <el-date-picker-->
      <!--            v-model="queryParams.tradeDate"-->
      <!--            type="date"-->
      <!--            value-format="YYYY-MM-DD"-->
      <!--            placeholder="请选择交易日期"-->
      <!--            clearable-->
      <!--            style="width: 240px"-->
      <!--        />-->
      <!--      </el-form-item>-->

      <el-form-item label="证券代码" prop="stockCode">
        <el-input
            v-model="queryParams.stockCode"
            placeholder="请输入证券代码"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证券名称" prop="stockName">
        <el-input
            v-model="queryParams.stockName"
            placeholder="请输入证券名称"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号标签" prop="accountTag">
        <el-select v-model="queryParams.accountTag" placeholder="请选择股票账号标签" clearable style="width: 240px">
          <el-option
              v-for="dict in stock_account_tag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="Refresh"-->
      <!--          @click="handleFullSync"-->
      <!--      >从{{ startDate }}开始生成持仓数据-->
      <!--      </el-button>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="statPositionList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="生成日期" align="center" prop="tradeDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.tradeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号标签" align="center" prop="accountTag">
        <template #default="scope">
          <dict-tag :options="stock_account_tag" :value="scope.row.accountTag"/>
        </template>
      </el-table-column>
      <el-table-column label="证券代码" align="center" prop="stockCode"/>
      <el-table-column label="证券名称" align="center" prop="stockName"/>
      <el-table-column label="持有数量" align="center" prop="quantity"/>
      <el-table-column label="买入金额" align="center" prop="amount"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">

        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

  </div>
</template>

<script setup>
import {listStatPosition} from "@/api/stock/stat_position";
import {formatDate} from "@vueuse/core"; // listStatSettlement

const {proxy} = getCurrentInstance();
const {stock_account_tag} = proxy.useDict('stock_account_tag');

const statPositionList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    // tradeDate: null,
    accountNo: null,
    stockCode: null,
    stockName: null,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

const today = new Date();
const thirtyDaysAgo = new Date(today);
thirtyDaysAgo.setDate(today.getDate() - 30);
const startDate = formatDate(thirtyDaysAgo, 'YYYY-MM-DD');

/** 生成统计数据 */
// function handleFullSync() {
//   proxy.$modal.confirm('是否确认执行生成统计数据?').then(function () {
//     generateStat({start_date: startDate});
//     proxy.$modal.msgSuccess("已提交生成统计数据请求");
//   }).catch(() => {
//   });
// }

/** 查询股票交易统计列表 */
function getList() {
  loading.value = true;
  listStatPosition(queryParams.value).then(response => {
    statPositionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

getList();
</script>